package com.gopay.bis.ccb.bcdl.operation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.exception.GopayException;

public class SocketTransport {
	 /** socket客户端 ***/
    private Socket socketClient = null;
    protected Log log = LogFactory.getLog(this.getClass());

    // 请求地址
    private String host;
    // 请求端口
    private int port;
    // 超时时间
    private int timeout;
    // 接收块大小
    private int receiveBufferSize;
    // 发送块大小
    private int sendBufferSize;
    
    /***
     * 与银行交易
     * 
     * @throws SocketException
     * @throws SocketTimeoutException
     */
    public Object submit(Object obj) throws GopayException {
		try {
			socketClient = getSocket();
			String result = null;
			if (socketClient != null) {
				result = sendAndGetDataFromBankDL(socketClient, obj);
			}
			return result;
		} catch (Exception e) {
			//e.printStackTrace();
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6047);
    	}
    }

    /**
     * 与银行换取连接socket
     * 
     * @return
     * @throws SocketException
     * @throws SocketTimeoutException
     */
    private Socket getSocket() throws Exception {
        Socket socket = new Socket(host, port);
            socket.setSoTimeout(timeout);
            socket.setReceiveBufferSize(receiveBufferSize);
            socket.setSendBufferSize(sendBufferSize);
        return socket;
    }

    /**
     * socket通信 发送和接收报文
     * 
     * @param client
     * @param obj
     * @return String
     * @throws IOException
     * @throws GopayException 
     */
    private String sendAndGetDataFromBankDL(Socket client, Object obj) throws IOException, GopayException {
        BufferedWriter bw = null;
        OutputStreamWriter ow = null;
        StringBuffer stringBuffer = new StringBuffer();
        String result ="";
        try {
        	 String send = (String)obj;
            // 提交报文
            ow = new OutputStreamWriter(client.getOutputStream(),"GB18030");
            bw = new BufferedWriter(ow);
            bw.write(send);
            bw.flush();
            // 接收返回内容，根据文档直接是个字符串
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(),"GB18030"));
            String s = "";
            while ((s = reader.readLine()) != null) {
                stringBuffer.append(s);
            }
            result =stringBuffer.toString();
        } catch (IOException e) {
            log.info("ccb bcdl SocketTransport error");
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6047);
        } finally {
            if (bw != null) {
                bw.close();
            }
            if (ow != null) {
                ow.close();
            }
            if(socketClient!=null&&socketClient.isConnected()){
            	socketClient.close();
            }
        }
        return result;
    }


	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public int getReceiveBufferSize() {
		return receiveBufferSize;
	}

	public void setReceiveBufferSize(int receiveBufferSize) {
		this.receiveBufferSize = receiveBufferSize;
	}

	public int getSendBufferSize() {
		return sendBufferSize;
	}

	public void setSendBufferSize(int sendBufferSize) {
		this.sendBufferSize = sendBufferSize;
	}
}